package com.yuyou.fn.platform.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.platform.entity.SysDataPermission;
import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.platform.mapper.SysDataPermissionMapper;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.platform.service.ISysDataPermissionService;
import com.yuyou.fn.platform.service.ISysUserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * SysDataPermission 表数据服务层接口实现类
 *
 */
@Service
public class SysDataPermissionServiceImpl extends SuperServiceImpl<SysDataPermissionMapper, SysDataPermission,Long> implements ISysDataPermissionService {

    @Resource
    private ISysUserService sysUserService ;

    @CacheEvict(value = { "cache_findDataPermissionForUser" ,"cache_findDataPermission" },allEntries = true)
    @Override
    @Transactional
    public void saveDataPermission(Long userId, List<SysDataPermission> sysDataPermissionList) {

        if(userId == null ){
            throw new NullPointerException();
        }
        Example<SysDataPermission> example = SysDataPermission.createExample();
        example.createCriteria().andEqualTo("userId",userId );
        deleteByExample(example);

        if(sysDataPermissionList != null && !sysDataPermissionList.isEmpty()){
            for(SysDataPermission sysDataPermission : sysDataPermissionList ){
                sysDataPermission.setUserId(userId);
            }
            insertBatch(sysDataPermissionList) ;
        }

    }




    @Cacheable("cache_findDataPermission")
    @Override
    public List<SysDataPermission> findDataPermission(Long userId) {
        Example<SysDataPermission> example = SysDataPermission.createExample();
        example.createCriteria().andEqualTo("userId",userId );
        return findListByExample(example);
    }

    @Cacheable("cache_findDataPermissionForUser")
    @Override
    public DataPermission findDataPermissionForUser(Long userId) {

        SysUser sysUser = sysUserService.findByUserIdFromCache(userId);
        if (sysUser==null){
            return new DataPermission(null,null,null,null);
        }
        List<SysDataPermission> list = findDataPermission(userId);
        List<Long> areaTeamIds = new ArrayList<>(list.size());
        List<Long> busTeamIds = new ArrayList<>(list.size());
        List<Long> campusIds = new ArrayList<>(list.size());

        List<Long> gradeIds=new ArrayList<>(list.size());
        List<Long> subjectIds=new ArrayList<>(list.size());

        for(SysDataPermission sysDataPermission : list ){
            if(sysDataPermission.getResourceType() == 0 ){
                areaTeamIds.add(sysDataPermission.getResourceId());
            }else if(sysDataPermission.getResourceType() == 1 ){
                busTeamIds.add(sysDataPermission.getResourceId());
            }else if(sysDataPermission.getResourceType() == 2 ){
                campusIds.add(sysDataPermission.getResourceId());
            }else if (sysDataPermission.getResourceType()==3){
                gradeIds.add(sysDataPermission.getResourceId());
            }else if (sysDataPermission.getResourceType()==4){
                subjectIds.add(sysDataPermission.getResourceId());
            }
        }
        DataPermission dataPermission = new DataPermission(sysUser.getUserType(), areaTeamIds, busTeamIds, campusIds);
        dataPermission.setGradeIds(gradeIds);
        dataPermission.setSubjectIds(subjectIds);
        return dataPermission;
    }

    @CacheEvict(value = { "cache_findDataPermissionForUser" ,"cache_findDataPermission"  },allEntries = true)
    @Override
    public void clearCache() {

    }

    @Override
    public void deleteByResourceTypes(List<Integer> resourceTypes) {
        if (Utils.isEmpty(resourceTypes)){
            return;
        }
        Example<SysDataPermission> example=SysDataPermission.createExample();
        example.createCriteria().andIn("resourceType",resourceTypes);
        this.deleteByExample(example);
    }
}